GO
/****** Object:  StoredProcedure [dbo].[cs_user_CreateUpdateDelete]    Script Date: 04/11/2007 17:25:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO


ALTER  procedure [dbo].[cs_user_CreateUpdateDelete] 
(
	@cs_UserID int out,
	@UserID uniqueidentifier = null,
	@UserName   nvarchar (64) = '',
	@PropertyNames ntext = null,
	@PropertyValues ntext = null,
	@UserAccountStatus  smallint = 1,
	@IsAnonymous   smallint = 0,
	@IsIgnored     bit = 0,
	@AppUserToken       varchar (128) = '',
	@ForumView   int = 0,
	@TimeZone   float = 0.0,
	@PostRank   binary(1) = 0x0,
	@PostSortOrder   int = 0,
	@IsAvatarApproved   smallint  = 0,
	@ForceLogin   bit   = 0,
	@ModerationLevel  smallint  = 0,
	@EnableThreadTracking  smallint  = 0,
	@EnableDisplayUnreadThreadsOnly smallint  = 0,
	@EnableAvatar    smallint  = 0,
	@EnableDisplayInMemberList  smallint  = 1,
	@EnablePrivateMessages  smallint  = 1,
	@EnableOnlineStatus smallint  = 1,
	@EnableEmail smallint  = 1,
	@EnableHtmlEmail   smallint  = 1,
	@FavoritesShared int = 0,
	@AllowSiteToContact bit = 0,
	@AllowSitePartnersToContact bit = 0,
	@SettingsID int,
	@Action    int,	
	@SkillLevel int=0,--EUPA code
	@Address nvarchar(150) ='''',--EUPA code
	@PostalCode nvarchar(7) ='''',--EUPA code
	@Province nvarchar(20) ='''',--EUPA code
	@Male bit=1,--EUPA code
	@Bday smalldatetime,--EUPA code
	@Email nvarchar(50),--EUPA code
	@Name nvarchar(100), --EUPA code
	@UpdateAllSettingsID bit = 0
)
AS
/*
This sproc returns an int that indicates the CreateUserStatus enum value:
	UnknownFailure = 0
	Created = 1
	DuplicateUsername = 2
	DuplicateEmailAddress = 3
	InvalidFirstCharacter = 4
	DisallowedUsername = 5
	Updated = 6
	Deleted = 7
	InvalidQuestionAnswer = 8
	InvalidPassword = 9
*/

-- first, we need to check if the username is a dup

-- Are we creating a user?
IF @Action = 0
BEGIN
	IF (@IsAnonymous = 1)
	BEGIN
		SELECT @cs_UserID = cs_UserID FROM cs_vw_Users_FullUser WHERE UserName = @UserName AND IsAnonymous = 1 AND SettingsID = @SettingsID
		
		-- Check if the anonymous user already exists
		IF @cs_UserID IS NOT NULL
		Begin
			SELECT 1
			RETURN
		End
		
	END
	ELSE
	BEGIN
		--A WSHA User can exist in multiple sites. We need to find all the site's this user will have access to
		-- and create the coresponding records
		Declare @ApplicationName nvarchar(256)
		Select @ApplicationName = ApplicationName FROM cs_SiteSettings where SettingsID = @SettingsID

		INSERT INTO [cs_Users]([MembershipID], [ForceLogin], [UserAccountStatus], [AppUserToken], [LastActivity], [LastAction])
		VALUES(@UserID, @ForceLogin,@UserAccountStatus, @AppUserToken, getdate(), '')

		Select @cs_UserID = @@Identity

		INSERT INTO cs_UserProfile ( [UserID], [TimeZone], [TotalPosts], 	[PostSortOrder], [PostRank], [IsAvatarApproved], [ModerationLevel], [EnableThreadTracking], [EnableDisplayUnreadThreadsOnly], [EnableAvatar], [EnableDisplayInMemberList], [EnablePrivateMessages], [EnableOnlineStatus], [EnableEmail], [EnableHtmlEmail], [IsIgnored], [MembershipID], [SettingsID], [PropertyNames], [PropertyValues], [FavoritesShared], [AllowSiteToContact], [AllowSitePartnersToContact])
		Select @cs_UserID, @TimeZone, 0, @PostSortOrder, @PostRank, @IsAvatarApproved, @ModerationLevel, @EnableThreadTracking,  @EnableDisplayUnreadThreadsOnly,  @EnableAvatar,   @EnableDisplayInMemberList, @EnablePrivateMessages,   @EnableOnlineStatus,  @EnableEmail, @EnableHtmlEmail, @IsIgnored,  @UserID, SettingsID, @PropertyNames, @PropertyValues, @FavoritesShared, @AllowSiteToContact, @AllowSitePartnersToContact
		FROM cs_SiteSettings where ApplicationName = @ApplicationName
		
		--EUPA code
		INSERT INTO Player ( [UserID],[LastName],[Address],[Province],[PostalCode],[SkillLevel],[Male],[BirthDate],[Email],[isUpdated])
		Select @UserID,@Name,@Address,@Province,@PostalCode,@SkillLevel,@Male,@Bday,@Email,0
		
		Select 1
		RETURN
	END
END
ELSE
BEGIN

	UPDATE
		cs_UserProfile
	SET
		TimeZone = @TimeZone,
		PostRank = @PostRank,
		PostSortOrder = @PostSortOrder,
		PropertyNames = @PropertyNames,
		PropertyValues = @PropertyValues,
		IsAvatarApproved = @IsAvatarApproved,
		ModerationLevel = @ModerationLevel,
		EnableThreadTracking = @EnableThreadTracking,
		EnableDisplayUnreadThreadsOnly = @EnableDisplayUnreadThreadsOnly,
		EnableAvatar = @EnableAvatar,
		EnableDisplayInMemberList = @EnableDisplayInMemberList,
		EnablePrivateMessages = @EnablePrivateMessages,
		EnableOnlineStatus = @EnableOnlineStatus,
		EnableEmail = @EnableEmail,
		EnableHtmlEmail = @EnableHtmlEmail,
		FavoritesShared = @FavoritesShared,
		AllowSiteToContact = @AllowSiteToContact,
		AllowSitePartnersToContact = @AllowSitePartnersToContact,
		IsIgnored = @IsIgnored,
		SkillLevel = @SkillLevel --EUPA Code

	WHERE
		UserID = @cs_UserID
		AND (SettingsID = @SettingsID or @UpdateAllSettingsID = 1)


	Declare @CurrentSkillLevel int
	Declare @CurrentAddress nvarchar (150)
	Declare @CurrentPostalCode nvarchar(7)
	Declare @CurrentProvince nvarchar(20)
	Declare @CurrentMale bit
	Declare @CurrentBDay smalldatetime

	select @CurrentSkillLevel = SkillLevel, @CurrentAddress = Address, @CurrentPostalCode = PostalCode, @CurrentProvince = Province, @CurrentMale = Male, @CurrentBDay = Birthdate
	from player
	WHERE UserID = @UserID

	IF @SkillLevel = @CurrentSkillLevel and @Address = @CurrentAddress and @PostalCode = @CurrentPostalCode and @Province = @CurrentProvince and @Male = @CurrentMale and @BDay = @CurrentBDay
	Begin
		--Update EUPA 'Same'
		UPDATE
			player
		SET
			SkillLevel = @SkillLevel,
			Address = @Address,
			PostalCode = @PostalCode,
			Province = @Province,
			Male = @Male,
			Birthdate = @BDay,
			Email = @Email,
			[LastName] = @Name
		WHERE
			UserID = @UserID
	END
	Else
	BEGIN
		--Update EUPA 'different'
		UPDATE
			player
		SET
			SkillLevel = @SkillLevel,
			Address = @Address,
			PostalCode = @PostalCode,
			Province = @Province,
			Male = @Male,
			Birthdate = @BDay,
			isUpdated = 1,
			Email = @Email,
			[LastName] = @Name
		WHERE
			UserID = @UserID
	END

	

	-- Update the user's anonymous settings passed in, as well.
	UPDATE aspnet_Users
	SET IsAnonymous = @IsAnonymous
	WHERE UserId = @UserID
  
	-- Some properties will be created/defaulted by SHS Create User.
	-- These values will be updated by Membership.UpdateUser as well. 
	-- Here we just update the properties which fall outside of MembershipUser
	-- First Update the cs_vw_forums_Users table

	UPDATE
		cs_Users
	SET
		AppUserToken = @AppUserToken,
		UserAccountStatus = @UserAccountStatus,
		ForceLogin = @ForceLogin
	WHERE
		 MembershipID = @UserID

	SELECT 6
END 